package com.nosleep.nosleep;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.plugin.Plugin;


public class nosleep implements Listener {
    Plugin con = com.nosleep.main.getPlugin(com.nosleep.main.class);
    @EventHandler
    public void nosleep(PlayerBedEnterEvent event){
        Player player = event.getPlayer();
        int time = Math.toIntExact(player.getPlayerTime());
        if(con.getConfig().getBoolean("Time.Enable")){
            int hourss = con.getConfig().getInt("Time.StartTime");
            int minutes = con.getConfig().getInt("Time.StopTime");
            int t1 = con.getConfig().getInt("Time.StartTime");
            int t2 = con.getConfig().getInt("Time.StopTime");
            if (hourss<=5&&minutes<=5){
                t1 = con.getConfig().getInt("Time.StartTime")+18;
                t2 = con.getConfig().getInt("Time.StopTime")+18;
            }else if (hourss<=5){
                t1 = con.getConfig().getInt("Time.StartTime")+18;
            }else if (minutes<=5){
                t2 = con.getConfig().getInt("Time.StopTime")+18;
            }else {
                t1 = con.getConfig().getInt("Time.StartTime")-6;
                t2 = con.getConfig().getInt("Time.StopTime")-6;
            }
            String hours1 = hourss + "─" + minutes;
            if (con.getConfig().getBoolean("Time.TimeChattext")){
                int hours = t1*1000;
                int minute = t2*1000;
                String tm = con.getConfig().getString("Time.TimeMessage");
                if (hourss>=25||minutes>=25||hourss<0||minutes<0||minutes<hourss){
                    player.sendMessage("§4error:§c请联系管理员检查开始时间与停止时间！");
                    event.setCancelled(true);
                }
                if (hours>minute&&minute>time&&hours<time){
                    player.sendMessage(tm.replace("%time%",hours1));
                    event.setCancelled(true);
                }else if (hours<minute&&minute>time&&hours<time){
                    player.sendMessage(tm.replace("%time%",hours1));
                    event.setCancelled(true);
                }
            }else {
                int hours = t1*1000;
                int minute = t2*1000;
                if (hourss>=25||minutes>=25||hourss<0||minutes<0||minutes<hourss){
                    player.sendMessage("§4error:§c请联系管理员检查开始时间与停止时间！");
                    event.setCancelled(true);
                }
                if (hours>minute&&minute>time&&hours<time){
                    event.setCancelled(true);
                }else if (hours<minute&&minute>time&&hours<time){
                    event.setCancelled(true);
                }
            }
        } else if (con.getConfig().getBoolean("Message")){
            player.sendMessage(con.getConfig().getString("Chattext"));
            event.setCancelled(true);
        }else{
            event.setCancelled(true);}
    }

}

